package com.salesmanager.core.business.product.dao;

import com.salesmanager.core.business.generic.dao.SalesManagerEntityDaoImpl;
import com.salesmanager.core.business.product.Product;
import com.salesmanager.core.business.statistics.model.ProductCriteria;
import com.salesmanager.web.model.paging.PaginationDataList;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import javax.persistence.Query;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by LiuTong on 2017/11/21.
 */
@Repository("productDao")
public class ProductDaoImpl extends SalesManagerEntityDaoImpl<Long,Product> implements ProductDao  {
    @Override
    public PaginationDataList<Product> getProductList(ProductCriteria criteria) {
        StringBuilder sql = new StringBuilder();
        StringBuilder countSql = new StringBuilder();
        StringBuilder whereSql = new StringBuilder();

        sql.append("SELECT P.PRODUCT_ID,P.PRODUCT_NAME,P.COUNT,P.STATUS,P.PRODUCT_CODE FROM PRODUCT P WHERE 1=1 ");
        countSql.append("SELECT COUNT(*) FROM PRODUCT P WHERE 1=1 ");

        if (!StringUtils.isBlank(criteria.getProductCode())){
            whereSql.append("AND P.PRODUCT_CODE = :code ");
        }
        if (!StringUtils.isBlank(criteria.getProductName())){
            whereSql.append("AND P.PRODUCT_NAME LIKE :name ");
        }

        if (criteria.getProductId() != null){
            whereSql.append("AND P.PRODUCT_ID = :id ");
        }

        Query query = super.getEntityManager().createNativeQuery(sql.append(whereSql).toString());
        Query countQuery = super.getEntityManager().createNativeQuery(countSql.append(whereSql).toString());

        //分页
        if (criteria.getMaxCount() > 0){
            query.setMaxResults(criteria.getMaxCount());
            query.setFirstResult(criteria.getStartIndex());
        }

        if (!StringUtils.isBlank(criteria.getProductCode())){
            query.setParameter("code", criteria.getProductCode());
            countQuery.setParameter("code", criteria.getProductCode());
        }
        if (!StringUtils.isBlank(criteria.getProductName())){
            query.setParameter("name", "%"+criteria.getProductName()+"%");
            countQuery.setParameter("name", "%"+criteria.getProductName()+"%");
        }

        if (criteria.getProductId() != null){
            query.setParameter("id", criteria.getProductId());
            countQuery.setParameter("id", criteria.getProductId());
        }

        PaginationDataList<Product> result = new PaginationDataList<Product>();
        //总数
        Number totalCount = (Number)countQuery.getSingleResult();
        result.setTotalCount(totalCount.intValue());

        List lists = query.getResultList();
        List<Product> data = new ArrayList<Product>();
        for(int i=0;i<lists.size();i++){
            Object[] o = (Object[]) lists.get(i);
            Product product = new Product();
            product.setId(Long.parseLong(o[0].toString()));
            product.setProductName((String) o[1]);
            product.setCount(Integer.parseInt(o[2].toString()));
            product.setStatus((Integer)o[3]);
            product.setProductCode((String) o[4]);
            data.add(product);
        }
        result.setData(data);
        return result;
    }
}
